<?php
class Mod_phongkham extends CI_Controller{
    function  __construct(){
        parent::__construct();
        $this->load->helper(array('form', 'url','captcha'));
		$this->load->library('form_validation','session');
        $this->load->model('mod_phongkham/mmod_phongkham');
    }
    function index($datapk,$page = 'hd'){
        if(!$this->session->userdata('hitpk-'.$datapk->id)){
            $hits = $datapk->hits + 1;
            $this->mmod_phongkham->updatehits($datapk->id,$hits);
            $this->session->set_userdata('hitpk-'.$datapk->id,$hits);
        }
        
        $data['phongkham'] = $datapk;
        $data['pkpage'] = $page;
        // page gioi thieu
        $data['like'] = count($this->home_model->get_data('like',array('idphongkham'=>$datapk->id,'type'=>0)));
        if ($page == 'gt'){
            $this->load->view('mod_phongkham/vmod_phongkham',$data);
        }
        if ($page == 'hd'){  // page hoi dap
            $data['cauhoi'] = $this->home_model->get_data('cauhoi',array('idphongkham'=>$datapk->id,'publish'=>1),array('truong'=>'id','sapxep'=>'desc'),3);
            if($data['cauhoi'][0])
                $idcauhoi = $data['cauhoi'][0]->id;
            else 
                $idcauhoi = '';
            $data['introch'] = $this->load->controller('mod_phongkham/mod_phongkham/hoidap',array($idcauhoi),true);
            $this->load->view('mod_phongkham/vmod_phongkham',$data);
        }
	}
    function hoidap($param = ''){
        // Xu ly cau hoi va cau tra loi
        if($param != '' || isset($_POST['idch'])) {
            $idcauhoi = ($param != '')?$param:$_POST['idch'];
            $this->session->set_userdata(array('idcauhoi'=>$idcauhoi));
            $data['cauhoi'] = $this->home_model->get_one('cauhoi',array('id'=>$idcauhoi));
            $data['traloi'] = $this->home_model->get_data('traloi',array('idcauhoi'=>$idcauhoi,'publish'=>1),array('truong'=>'id','sapxep'=>'desc'));
            $data['binhluan'] = $this->home_model->get_data('binhluan',array('idcauhoi'=>$idcauhoi,'publish'=>1),array('truong'=>'id','sapxep'=>'desc'),5);
            $iduser = $data['cauhoi']->iduser;
            $data['user'] = $this->home_model->get_one('users',array('id'=>$iduser));
        }else{
            $data['cauhoi'] = $data['traloi'] = $data['binhluan'] = '';
        }
        $data['captcha'] = $this->captchacm();
        
        // Load view
        $this->load->view('mod_phongkham/cauhoi',$data);
    }
    function xulyform(){
        $this->load->library('session');
        // Khai bao form binh luan
        $this->form_validation->set_rules('name', 'Họ tên', 'trim|required|max_length[200]|xss_clean');
        $this->form_validation->set_rules('address', 'Địa chỉ', 'required|min_length[8]|max_length[200]|trim|xss_clean');
        $this->form_validation->set_rules('phone', 'Điện thoại', 'required|min_length[8]|max_length[200]|trim|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'required|min_length[8]|valid_email|trim|xss_clean');
        $this->form_validation->set_rules('comment', 'Nội dung', 'required|min_length[50]|max_length[2000]|trim|xss_clean');
        $this->form_validation->set_rules('captcha', 'Mã bảo mật', 'trim|xss_clean|required');

        $captcha= $this->session->userdata('captcha');
        if ($this->form_validation->run() == FALSE || $_POST["captcha"] != $captcha['word']){
            $thongbao = '<p style="color:#f00">Đã có lỗi xảy ra.</p>';
            if ($this->form_validation->run() == FALSE){
                $thongbao .= validation_errors();
            }
            if($_POST["captcha"] != $captcha['word']){
                $thongbao .= '<p>Mã bảo mật nhập sai.</p>';
            }
        }
        else{
            $this->load->model('mod_phongkham/mmod_phongkham');
            $idcauhoi = $this->session->userdata('idcauhoi');
            $arr = array(
                'idcauhoi'=>$idcauhoi,
                'name'=>$_POST["name"],
                'address'=>$_POST["address"],
                'phone'=>$_POST["phone"],
                'email'=>$_POST["email"],
                'content'=>$_POST["comment"],
                'ip'=>$_SERVER['REMOTE_ADDR']
            );
            $this->mmod_phongkham->insert_data($arr);
            $thongbao = 'Bình luận của bạn đã được gửi đi<br/> Bình luận của bạn sẽ được kiểm duyệt trước khi được đăng lên.';
        }
        echo $thongbao;
    }
    function captchacm(){
        $this->load->library('session');
        $this->load->helper('captcha');
        $vals = array(             
            'font_path' => './template/font/Mo.m.ttf',
            'img_width' => '150',
            'img_height' => 30,
            'expiration' => 7200,
            'img_path' => './upload/images/captcha/',
            'img_url' => base_url().'/upload/images/captcha/'
        );
        $cap = create_captcha($vals);  
        $this->session->set_userdata(array('captcha'=>$cap));
        if(isset($_POST['load']) && $_POST['load'] == 'return'){
            echo $cap['image'];
        }
        return $cap['image'];
    }
}